package com.hellowx.qydcdev.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hellowx.qydcdev.dao.*;
import com.hellowx.qydcdev.entity.*;
import com.hellowx.qydcdev.service.BusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created on 2022/1/5.
 *
 * @author Hello WX
 */
@Service("businessService")
public class BusinessServiceImpl implements BusinessService {

    @Resource
    private BusinessDao businessDao;
    @Resource
    private GoodsCategoryDao goodsCategoryDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private StateDao stateDao;
    @Resource
    private UserDao userDao;
    @Resource
    private OrderPayDao orderPayDao;

    @Override
    public PageInfo<BusinessVO> selectListByPage(Page<Business> page) {

        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<Business> businessList = new LambdaQueryChainWrapper<>(businessDao).list();
        List<BusinessVO> businessVOList = new ArrayList<>();
        PageInfo<Business> businessPageInfo = new PageInfo<>(businessList);
        if (businessList!=null){
            businessList.forEach(business -> {
                BusinessVO businessVO = new BusinessVO();
                BeanUtils.copyProperties(business,businessVO);
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                int businessMonthlySales = 0;
                Map<String, Object> map = orderPayDao.countOrderGoodsByMonthAndBusinessId(business.getId(), sdf.format(date));
                if (map!=null && !map.isEmpty()){
                    businessMonthlySales = Integer.parseInt(String.valueOf(map.get("number")));
                }
                businessVO.setMonthlySales(businessMonthlySales);
                businessVOList.add(businessVO);
            });
        }
        PageInfo<BusinessVO> businessVOPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(businessPageInfo,businessVOPageInfo);
        businessVOPageInfo.setList(businessVOList);
        return businessVOPageInfo;
    }

    @Override
    public BusinessVO getBusinessIncludeGoodsById(Long businessId) {
        Business business = businessDao.selectById(businessId);
        BusinessVO businessVO = new BusinessVO();
        if (business!=null){
            BeanUtils.copyProperties(business,businessVO);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            int businessMonthlySales = 0;
            Map<String, Object> map = orderPayDao.countOrderGoodsByMonthAndBusinessId(business.getId(), sdf.format(date));
            if (map!=null && !map.isEmpty()){
                businessMonthlySales = Integer.parseInt(String.valueOf(map.get("number")));
            }
            businessVO.setMonthlySales(businessMonthlySales);
            List<GoodsCategory> goodsCategoryList = new LambdaQueryChainWrapper<>(goodsCategoryDao)
                    .eq(GoodsCategory::getBusinessId, businessId)
                    .list();
            List<GoodsCategoryVO> goodsCategoryVOList = new ArrayList<>();
            if (goodsCategoryList!=null){
                goodsCategoryList.forEach(goodsCategory -> {
                    GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
                    BeanUtils.copyProperties(goodsCategory,goodsCategoryVO);
                    List<Goods> goodsList = new LambdaQueryChainWrapper<>(goodsDao)
                            .eq(Goods::getBusinessId, businessId)
                            .eq(Goods::getCategoryId, goodsCategory.getId())
                            .eq(Goods::getStatus,0)
                            .list();
                    List<GoodsVO> goodsVOList = new ArrayList<>();
                    if (goodsList!=null){
                        goodsList.forEach(goods -> {
                            GoodsVO goodsVO = new GoodsVO();
                            BeanUtils.copyProperties(goods,goodsVO);
                            List<State> stateList = new LambdaQueryChainWrapper<>(stateDao)
                                    .eq(State::getGoodsId, goods.getId())
                                    .list();
                            int monthlySales = 0;
                            Map<String, Object> map2 = orderPayDao.countGoodsMonthlySales(goods.getId(), sdf.format(date));
                            if (map2!=null && !map2.isEmpty()){
                                monthlySales = Integer.parseInt(String.valueOf(map2.get("number")));
                            }
                            goodsVO.setMonthlySales(monthlySales);
                            goodsVO.setStateList(stateList);
                            goodsVOList.add(goodsVO);
                        });
                    }
                    goodsCategoryVO.setGoodsList(goodsVOList);
                    goodsCategoryVOList.add(goodsCategoryVO);
                });
            }
            businessVO.setGoodsCategoryList(goodsCategoryVOList);
        }
        return businessVO;
    }

    @Override
    public PageInfo<Business> listBusinessByNameAndPage(String businessName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Business> list = new LambdaQueryChainWrapper<>(businessDao)
                .like(Business::getName, businessName)
                .list();
        return new PageInfo<>(list);
    }

    @Override
    public BusinessVO getByIdIncludeUser(Long businessId) {
        Business business = new LambdaQueryChainWrapper<>(businessDao)
                .eq(Business::getId, businessId)
                .one();
        BusinessVO businessVO = new BusinessVO();
        BeanUtils.copyProperties(business,businessVO);
        User user = new LambdaQueryChainWrapper<>(userDao)
                .eq(User::getBusinessId, businessId)
                .one();
        businessVO.setUser(user);
        return businessVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveBusiness(Business business) {
        String msg;
        if (business.getId()==null){
            businessDao.insert(business);
            msg = "新增成功,请添加用户信息.";
        }else {
            businessDao.updateById(business);
            msg = "修改成功";
        }
        return msg;
    }
}
